package com.live.video.exception;

import com.live.video.enums.ResultEnum;
import com.live.video.util.Result;
import com.live.video.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Description:
 * User: godd
 * Date: 2017-10-11
 * Time: 下午3:43
 */
@ControllerAdvice
public class ExceptionHandle {
    private static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handle(Exception e) {
        //如果匹配到的异常是自定义的异常,就返回对应的自定义错误信息
        if (e instanceof MyException) {
            MyException myException = (MyException) e;
            return ResultUtil.error(myException.getCode(), myException.getMessage());
            //其他运行时错误,直接返回错误
        } else {
            logger.error("[系统异常]", e);
            return ResultUtil.error(ResultEnum.ERRO.getCode(), ResultEnum.ERRO.getMsg());
        }
    }

}
